package com.lyu.travel.controller;

import com.lyu.travel.core.entity.User;
import com.lyu.travel.core.mapper.PlaceMapper;
import com.lyu.travel.core.mapper.UserMapper;
import com.lyu.travel.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: [travel]
 * @Package: [com.lyu.travel.controller]
 * @ClassName: [EmpsController]
 * @Description: 员工管理控制
 * @Author: [78483]
 * @CreateDate: [2020-06-11 16:57]
 * @UpdateUser: [78483]
 * @UpdateDate: [2020-06-11 16:57]
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;


    //用户管理
    @GetMapping("/emps")
    public String emps(Model model) {
        //业务处理逻辑
        model.addAttribute("empList", userService.findAll());
        return "empList";
    }

    //添加
    @PostMapping("/empSave")
    @ResponseBody
    public  Object saveEmps(User user){
        //处理请求
        Map map= new HashMap<>();
        if (userService.selectByUsername(user.getUsername())!=null){
            map.put("msg1", "用户名重复");
        }else if (user.getUsername()=="" || user.getPassword()=="" || user.getEmail()=="") {

            map.put("msg2","不能为空");
        }else{
            userService.insert(user);
            map.put("success",true);
            map.put("data",user);
        }
        return map  ;
    }

    //删除
    @PostMapping("/empDelete")
    @ResponseBody
    public int deleteEmps(int id){
        //处理请求
        return userService.deleteUserById(id);
    }

    //更新
    @PostMapping("/empUpdate")
    @ResponseBody
    public Object updateEmps(User user){
        //处理请求
        Map map= new HashMap<>();
        if (user.getUsername()=="" || user.getPassword()=="" || user.getEmail()=="") {
            map.put("msg2", "不能为空");
        }else{
            userService.updateUser(user);
        }
        return map  ;
    }
}
